//
//  CSSNetWorkingTools.swift
//  weibo28
//
//  Created by chushushan on 16/5/12.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import AFNetworking

enum RequestType: Int {
    //get请求
    case  GET = 0
    //post请求
    case POST = 1
}

class CSSNetWorkingTools: AFHTTPSessionManager {
    
    //继承自AFHTTPSessionManager的管理单例
    //单例
    static let sharedTools: CSSNetWorkingTools = {
    let tools = CSSNetWorkingTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
      
    }()
    
    
    //网络请求的封装 get/post
    func request(requestType: RequestType, url: String, params: AnyObject?, callBack: (response: AnyObject?, error: NSError?) -> ()) {
    //定义一个成功的闭包
        let successClosure = { (dataTasks: NSURLSessionDataTask, response: AnyObject?) -> Void in
            //成功的回调
            callBack(response: response, error: nil)
            
        }
        
        //定义一个失败的闭包
        let failureClosure = { (dataTasks: NSURLSessionDataTask?, error: NSError) -> Void in
           //失败的回调
            callBack(response: nil, error: error)
            
        }
    
        //方式
        if requestType == .GET {
        GET(url, parameters: params, progress: nil, success: successClosure, failure: failureClosure)
        
        } else {
        POST(url, parameters: params, progress: nil, success: successClosure, failure: failureClosure)
        }
    }
}



//发微博相关接口
extension CSSNetWorkingTools {

    func updata(access_token: String, status: String, callBack:(response: AnyObject?, error: NSError?) -> ()) {
        let url = "https://api.weibo.com/2/statuses/update.json"
        let params = [
            "access_token": access_token,
            "status": status
        ]
    
        request(.POST, url: url, params: params, callBack: callBack)
    
    }
}




//首页相关接口
extension  CSSNetWorkingTools {
//请求微博首页
    //max_id是上拉加载时的参数，我们给他个默认值
    func requsetStatus(accessToken: String, maxId: Int64 = 0, sinceId: Int64 = 0, callBack:(response: AnyObject?, error: NSError?) -> ()) {
    
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        let params = [
            "access_token": accessToken,
            "max_id": "\(maxId)",
            "since_id": "\(sinceId)"
            
        ]
        
        request(.GET, url: url, params: params, callBack: callBack)
    }

}



// OAuth相关接口
extension CSSNetWorkingTools {

    //获取accesstoken的接口
    func requestAccessToken(code: String, callBack: (response: AnyObject?, error: NSError?) -> ()) {
    
        let urlstr = "https://api.weibo.com/oauth2/access_token"
        //设置参数
        let params = [
            "client_id": WeiboAppKey,
            "client_secret": WeiboAppSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": WeiboRedirect_Url
        ]
        
        request(.POST, url: urlstr, params: params, callBack: callBack)
    
    }
    
    
    
//获取用户信息
    func requestUserInfo(userAccount: CSSUserAcount, callBack: (response: AnyObject?, error: NSError?) -> ()) {
    let url = "https://api.weibo.com/2/users/show.json"
        let params = [
        
            "access_token": userAccount.access_token!,
            "uid": "\(userAccount.uid)"
        ]
    
        request(.GET, url: url, params: params, callBack: callBack)
        
    }
}




























